package user

import (
	"common/md5"
	"common/mildware"
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"strconv"
)

// UpdateUserBaseInfo 更新用户信息
// @Summary 更新用户信息
// @Description 更新用户信息
// @Tags 用户
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object body docModel.DocUpdateBaseUserInfoReq false "更新参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /user/update/base [post]
func (h UHandler) UpdateUserBaseInfo(c *gin.Context) {
	var (
		idStr string
		err   error
		sexI  int
	)
	resp := new(models.RespData)
	req := new(models.UpdateBaseUserInfoReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("User:UpdateUserBaseInfo  with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("User:UpdateUserBaseInfo  with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	idStr, _, _, err = mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("User:UpdateUserBaseInfo UpdateUserInfo error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	sexI, err = strconv.Atoi(req.Sex)
	if err != nil {
		zap.L().Error("User:UpdateUserBaseInfo strconv.Atoi error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	UserInfo := &models.UserInfo{
		NickName: req.NickName,
		Phone:    req.Phone,
		Sex:      sexI,
	}
	if err := h.M.UpdatesUserByID(idStr, UserInfo); err != nil {
		zap.L().Error("User:UpdatesUserByID h.M.UpdatesUserByUid error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}

// UpdateUserPasswordInfo 重置密码
// @Summary 重置密码
// @Description 重置密码
// @Tags 用户
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object body docModel.DocUpdateUserPasswordReq false "更新参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /user/update/password [post]
func (h UHandler) UpdateUserPasswordInfo(c *gin.Context) {
	var (
		idStr string
		err   error
	)
	resp := new(models.RespData)
	req := new(models.UpdateUserPasswordReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("User:UpdateUserPasswordInfo with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("User:UpdateUserPasswordInfo with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	idStr, _, _, err = mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("User:UpdateUserPasswordInfo mildware.GetCyrrentUserID error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//获得用户信息
	userInfo, err := h.M.QueryUserInfoByID(idStr)
	if err != nil {
		zap.L().Error("User:UpdateUserPasswordInfo h.M.QueryUserInfoByID error", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//验证旧的密码
	if !md5.Compare(userInfo.Password, req.OldPassword) {
		resp.SendCode(c, models.CodeInvalidPassword)
		return
	}
	//判断新密码是否和原来密码一样
	if !md5.Compare(userInfo.Password, req.OldPassword) {
		resp.SendCode(c, models.CodeNewPasswordCopyOld)
		return
	}
	userInfo.Password = md5.MD5(req.Password)
	if err := h.M.SaveUserInfo(userInfo); err != nil {
		zap.L().Error("User:UpdateUserPasswordInfo h.M.SaveUserInfo error", zap.Error(err))
		resp.SendServerBusy(c)
	}
	resp.SendSuccess(c)
}
